public class TestDemo {


    /**
     * 面试的时候 不要写递归的斐波那契实现
     * 原因：重复计算的数据太多了 效率很低
     * @param n
     * @return
     */
    public static int fib(int n) {
        if (n == 1 || n == 2) {
            return 1;
        }
        return fib(n - 1) + fib(n - 2);
    }
    public static int fib2(int n){
        if (n == 1 || n == 2) {
            return 1;
        }
        int f1 = 1;
        int f2 = 1;
        int f3 = 0;
        for (int i = 3; i <= n; i++) {
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }
    public static void main(String[] args) {
        System.out.println(fib2(1));
        System.out.println(fib2(2));
        System.out.println(fib2(3));
        System.out.println(fib2(8));
    }

    public static void main8(String[] args) {
        System.out.println(fib(3));
    }
    //123的每一位相加
    public static int sumFunc(int n){
        if (n < 10){
            return n;
        }else {
            int ret = sumFunc(n / 10) + n % 10;
            return ret;
        }
    }
    public static void main7(String[] args) {
        System.out.println(sumFunc(1234));
    }


    public static void printf(int n){
        if (n < 10) {
            System.out.println(n);
        }else {
            printf(n / 10);
            System.out.println(n % 10);
        }
    }

    public static void main6(String[] args) {
        printf(4321);
    }
    public static int fac(int n){
        if(n == 1){
            return 1;
        }
        int tmp = n*fac(n-1);
        return tmp;
    }

    public static void print(int n){
        if (n == 1){
            System.out.println(n);
        }else {
            print(n - 1);
            System.out.println(n);
        }
    }
    public static void main5(String[] args) {
        print(9);
    }



    public static void func(int i){
        if (i == 1) return;
        func(i - 1);
    }

    public static void main4(String[] args) {
        func(10);
    }
    public static void main3(String[] args) {
        System.out.println(add(1, 2));
        System.out.println(add(1.2, 6.7));
    }
    public static int add(int x,int y){
        return x + y;
    }
    public static double add(double x,double y){
        return x + y;
    }

    public static void sum(int x1,int x2){
        System.out.println(x1 + x2);
    }

    public static void main2(String[] args) {
        sum(1,2);
    }
    public static void main1(String[] args) {
        int a = 10;
        int b = 20;
        int ret = add(a,b);
        System.out.println(ret);
    }
}